#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
 
int main() {
    int fd = open("example.txt", O_RDWR);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
 
    // 获取文件大小
    off_t file_size = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);
 
    // 映射文件到内存
    char *addr = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED) {
        perror("mmap failed");
        close(fd);
        return 1;
    }
 
    printf("%s",addr);
 
    // 解除映射并关闭文件
    munmap(addr, file_size);
    close(fd);
    return 0;
}